﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;

namespace MNNUTEM
{
    class crudCosto
    {


        private Conexion cn = new Conexion();
        String query;
        private mensaje msj;

        public void Insert(TextBox id, TextBox transporte, TextBox bodega, TextBox faltante, TextBox aduana, TextBox adquisicion, TextBox demora, TextBox idProducto)
        {
            try
            {
                query = "insert into Costo (idCosto,transporteCosto, bodegaCosto,faltanteCosto,aduanaCosto,adquisicionCosto,demoraCosto,Producto_idProducto) values ('" + id.Text + "','" + transporte.Text + "','" + bodega.Text + "','" + faltante.Text + "','" + aduana.Text + "','" + adquisicion.Text + "','" + demora.Text + "','" + idProducto.Text + "')";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
               
                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Acción realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }



        }
        public void Delete(int ID)
        {

            try
            {
                query = "delete from Costo where idCosto='" + ID + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
            
                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Acción realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }

        public void SetId(int ID, int idsuperior)
        {


            try
            {
                query = "UPDATE  Costo set idCosto='" + ID + "' where idCosto='" + idsuperior + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
             
                myCommand.ExecuteNonQuery();
                cn.conn().Close();


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }


        }


        public void Set(TextBox idCosto, TextBox transporte, TextBox bodega, TextBox faltante, TextBox aduana, TextBox adquisicion, TextBox demora, int idProducto)
        {


            try
            {
                query = "UPDATE  Costo set idCosto='" + idCosto.Text + "' , transporteCosto='" + transporte.Text + "', bodegaCosto='" + bodega.Text + "', faltanteCosto='" + faltante.Text + "', aduanaCosto='" + aduana.Text + "', adquisicionCosto='" + adquisicion.Text + "', demoraCosto='" + demora + "', Producto_idProducto='" + idProducto + "'  where idCosto='" + idCosto + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
               
                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Modificación realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }


        }

        public DataTable Consultation(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataTable dtable = new DataTable();
                sda.Fill(dtable);


                return dtable;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }


        public DataSet Consultation2(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataSet DS = new DataSet();
                sda.Fill(DS);


                return DS;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }


    }
}
